package cn.seocoo.platform.api.controller.user;

import cn.seocoo.platform.api.util.JSONResult;
import cn.seocoo.platform.api.util.PageBean;
import cn.seocoo.platform.car.dto.CarDto;
import cn.seocoo.platform.common.CommonServiceImpl;
import cn.seocoo.platform.user.domain.AppUser;
import cn.seocoo.platform.user.dto.UserInfo;
import cn.seocoo.platform.user.dto.UserTypeDto;
import cn.seocoo.platform.user.service.IAppUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @ClassName: ApiUserInfoController
 * @description: 用户个人信息
 * @author: wujian
 * @Date: 2019-12-30 18:41
 **/
@RequestMapping("api/user")
@RestController
public class ApiUserInfoController {


    @Autowired
    private IAppUserService iAppUserService;
    @Autowired
    private CommonServiceImpl commonService;

    /**
     * @return cn.seocoo.platform.api.util.JSONResult
     * @Author WuJian
     * @Description //校验用户类型
     * @Date 15:58 2019/12/30
     * @Param [appUserId]
     **/
    @GetMapping("/getUserInfo")
    public JSONResult getUserInfo(Integer appUserId) {
        UserInfo userInfo = iAppUserService.getApiUserInfo(appUserId);
        return JSONResult.ok(userInfo);
    }


    /**
     * @Author WuJian
     * @Description //修改保存个人信息
     * @Date 19:32 2019/12/30
     * @Param [appUserId, neckName, headImageUrl]
     * @return cn.seocoo.platform.api.util.JSONResult
     **/
    @GetMapping("/saveUserInfo")
    public JSONResult saveUserInfo(Integer appUserId, String nickName, String headImageUrl) {
        try {
            AppUser appUser = new AppUser();
            appUser.setId(appUserId);
            appUser.setNickName(nickName);
            appUser.setHeadImageUrl(headImageUrl);
            int row = iAppUserService.updateAppUser(appUser);
            if (row > 0) {
                return JSONResult.ok("修改成功");
            } else {
                return JSONResult.errorMsg(JSONResult.ERROR_MSG);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.errorMsg(JSONResult.ERROR_MSG);
        }

    }


    /**
     * @Author WuJian
     * @Description //我的发布
     * @Date 19:32 2019/12/30
     * @Param [appUserId, neckName, headImageUrl]
     * @return cn.seocoo.platform.api.util.JSONResult
     **/
    @GetMapping("/getMyCarList")
    public JSONResult getMyCarList(Integer appUserId, Integer pageNum,Integer pageSize) {
        try {
            if(appUserId == null){
                return JSONResult.errorMsg(JSONResult.NOT_FOUND_USER);
            }
            List<CarDto> carlist = iAppUserService.getCarList(pageNum, pageSize,appUserId);
            PageBean<CarDto> carDtoPageInfo = new PageBean<>(carlist);
            List<CarDto> list = carDtoPageInfo.getList();
            commonService.getCarListForServcie(list);
            return  JSONResult.ok(carDtoPageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.errorMsg(JSONResult.ERROR_MSG);
        }

    }







}
